package cn.liuyijiao;
import java.util.Scanner;

public class BaziCalculator {
    public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);

            // 用户输入出生年月日时
            System.out.println("请输入您的出生年份：");
            int year = scanner.nextInt();
            System.out.println("请输入您的出生月份：");
            int month = scanner.nextInt();
            System.out.println("请输入您的出生日期：");
            int day = scanner.nextInt();
            System.out.println("请输入您的出生时辰（整数，0表示子时，1表示丑时，以此类推）：");
            int hour = scanner.nextInt();

            // 计算八字
            String bazi = calculateBazi(year, month, day, hour);
            System.out.println("您的八字是：" + bazi);

            // 匹配阴阳五行
            String wuxing = matchWuxing(bazi);
            System.out.println("您的阴阳五行属性是：" + wuxing);

        scanner.close();
    }

    // 计算八字
    private static String calculateBazi(int year, int month, int day, int hour) {
        String[] tiangan = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
        String[] dizhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        String[] wuxing = {"木", "火", "土", "金", "水"};

        // 计算年份的天干和地支
        int tYear = (year - 4) % 10;
        int dYear = (year - 4) % 12;

        // 计算月份的天干和地支
        int tMonth = (dYear * 2 + month - 1) % 10;
        int dMonth = (month - 1) % 12;

        // 计算日期的天干和地支
        int tDay = (tMonth * 2 + day - 1) % 10;
        int dDay = (day - 1) % 12;

        // 计算时辰的天干和地支
        int tHour = (dDay * 2 + hour / 12) % 10;
        int dHour = hour % 12;

        // 根据计算结果拼接八字
        String baziStr = tiangan[tYear] + dizhi[dYear] + tiangan[tMonth] + dizhi[dMonth] + tiangan[tDay] + dizhi[dDay] + tiangan[tHour] + dizhi[dHour];

        // 完善八字的五行属性
        String tyearWuxing = wuxing[tYear % 5];
        String dyearWuxing = wuxing[dYear % 5];
        String tmonthWuxing = wuxing[tMonth % 5];
        String dmonthWuxing = wuxing[dMonth % 5];
        String tdayWuxing = wuxing[tDay % 5];
        String ddayWuxing = wuxing[dDay % 5];
        String thourWuxing = wuxing[tHour % 5];
        String dhourWuxing = wuxing[dHour % 5];

        baziStr += "（年：" + tyearWuxing + dyearWuxing+"，月：" + tmonthWuxing + dmonthWuxing+"，日：" + tdayWuxing+ ddayWuxing + "，时：" + thourWuxing + dhourWuxing+ "）";

        return baziStr;
    }

    // 匹配阴阳五行
    private static String matchWuxing(String bazi) {
        return bazi.substring(bazi.indexOf("（") + 1, bazi.indexOf("）"));
    }
}